package com.snsoft.web.controller;

import static com.snsoft.util.Constants.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.snsoft.bean.ResultMap;
import com.snsoft.bean.UpdatePasswordForm;
import com.snsoft.bean.UserSession;

/**
 * 
 * @author 慎伟康
 * 
 * @date 2017年12月22日 上午11:27:12
 * 
 * @Description TODO
 *    测试控制器，用于测试验证
 */
@Controller
public class TestController {
	
	@Autowired
	private HttpServletRequest request;
	
	@ResponseBody
	@RequestMapping(value = "/encodeTest", method = RequestMethod.GET, produces = "application/json")
	public ResultMap encodeTest(@RequestParam("var") String var){
		System.out.println("var-->" + var);
		System.out.println("var---->" + request.getParameter("var"));
		return new ResultMap(CODE_SUCCESS, MSG_OPERATE_SUCCESS, "测试中文");
	}
	
	@ResponseBody
	@RequestMapping(value = "/requestParamTest", method = RequestMethod.GET, produces = "application/json")
	public ResultMap requestParamTest(@RequestParam(value="var", defaultValue="") String var){
		System.out.println("var-->" + var + "length=" + var.length());
		return new ResultMap(CODE_SUCCESS, MSG_OPERATE_SUCCESS, "测试@RequestParam");
	}
	
	@ResponseBody
	@RequestMapping(value = "/formTest", method = RequestMethod.POST, produces = "application/json")
	public ResultMap formTest(@Valid UpdatePasswordForm updatePasswordForm, Errors errors){
		System.out.println("-->" + updatePasswordForm.toString());
		
		if(errors.hasErrors()){
			return new ResultMap(CODE_ERROR, errors.getFieldError().getDefaultMessage(), "测试表单数据填充失败");
		}
		
		return new ResultMap(CODE_SUCCESS, MSG_OPERATE_SUCCESS, "测试表单数据填充成功");
	}
	
	@ResponseBody
	@RequestMapping(value = "/book/{bookId}", method = RequestMethod.POST, produces = "application/json")
	public ResultMap pathVariableTest(@PathVariable("bookId") String bookId){
		System.out.println("bookId-->" + bookId + ", length=" + bookId.length());
		return new ResultMap(CODE_SUCCESS, MSG_OPERATE_SUCCESS, "测试@PathVariable");
	}
	
	@RequestMapping(value = "/viewTest", method = RequestMethod.GET)
	public String viewTest(){
		return "home";
	}
	
	@ResponseBody
	@RequestMapping(value = "/sessionTest", method = RequestMethod.POST)
	public ResultMap sessionTest(HttpSession session){
		System.out.println("session-->" + (UserSession) session.getAttribute("userSession"));
		return new ResultMap(CODE_SUCCESS, MSG_OPERATE_SUCCESS, "测试session");
	}
}
